package ${filePackageName};

import ${dtoPackage}.${upperEntityName}Dto;
import ${voPackage}.${upperEntityName}Vo;
import ${entityPackage}.${upperEntityName};
import ${mapperPackage}.${upperEntityName}Mapper;
import ${servicePackage}.${upperEntityName}Service;
import ${validatePackage}.EntityValidation;
import ${validatePackage}.ValidationType;
import ${exceptionPackage}.DaoException;
import ${exceptionPackage}.ValidateException;
import ${utilsPackage}.ConstantUtil;
import ${utilsPackage}.MessageConstant;
import ${utilsPackage}.MessageUtils;
import ${utilsPackage}.BeanUtil;
import ${beanCommonPackage}.Page;
import cn.hutool.core.util.ObjectUtil;${hutoolDatePackage}
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;${utilDatePackage}
import java.util.List;
import java.util.Map;${uuidPackage}

/**
 *  ${tableComment} service接口实现类
 *
 * @author ${projectAuthor}
 * @version 1.0
 * @date ${nowDate}
 */
@Service
public class ${upperEntityName}ServiceImpl implements ${upperEntityName}Service, EntityValidation<${upperEntityName}> {

	@Resource
	private ${upperEntityName}Mapper ${lowerEntityName}Mapper;

	/**
	 * 新增
	 *
	 * @param ${lowerEntityName} 新增的对象
	 * @return 新增条数
	 * @throws DaoException 数据库异常
	 * @throws ValidateException 数据库异常
	 */
	@Override
	public int insert(${upperEntityName} ${lowerEntityName}) throws DaoException, ValidateException {
		int result = 0;

		//校验
		this.validate(${lowerEntityName}, ValidationType.ADD);

		try {${setPrimaryKey}
			result = ${lowerEntityName}Mapper.insert(${lowerEntityName});
		} catch (Exception e){
			throw new DaoException(e, MessageUtils.get(MessageConstant.INSERT_FAILED));
		}

		return result;
	}

	/**
	 * 修改
	 *
	 * @param ${lowerEntityName} 修改的对象
	 * @return 修改条数
	 * @throws DaoException 数据库异常
	 * @throws ValidateException 数据库异常
	 */
	@Override
	public int update(${upperEntityName} ${lowerEntityName}) throws DaoException, ValidateException {
		int result = 0;

		//校验
		validate(${lowerEntityName}, ValidationType.UPDATE);

		try {${setDateKey}
			result = ${lowerEntityName}Mapper.updateById(${lowerEntityName});
		} catch (Exception e){
			throw new DaoException(e, MessageUtils.get(MessageConstant.UPDATE_FAILED));
		}

		return result;
	}

	/**
	 * 校验${upperEntityName}
	 * @param ${lowerEntityName}
	 * @param validationType
	 * @throws ValidateException
	 */
	@Override
	public void validate(${upperEntityName} ${lowerEntityName}, ValidationType validationType) throws ValidateException {
		//校验${upperEntityName}
	}

	/**
	 * 修改
	 *
	 * @param map
	 * @return 修改条数
	 * @throws DaoException 数据库异常
	 */
	@Override
	public int updateByMap(Map<String,Object> map) throws DaoException{
		int result = 0;

		try {
			result = ${lowerEntityName}Mapper.updateByMap(map);
		} catch (Exception e){
			throw new DaoException(e, MessageUtils.get(MessageConstant.UPDATE_FAILED));
		}

		return result;
	}

	/**
	 * 删除
	 *
	 * @param ${lowerPrimaryKey}s ${primaryKeyComment}数组
	 * @return 删除的条数
	 * @throws DaoException 数据库异常
	 * @throws ValidateException 数据校验异常
	 */
	@Override
	public int delete(${primaryType} ... ${lowerPrimaryKey}s) throws DaoException, ValidateException {
		int result = 0;

		if(ObjectUtil.isEmpty(${lowerPrimaryKey}s)){
			throw new ValidateException("${lowerPrimaryKey}s"+ MessageUtils.get(MessageConstant.PARAMETER_CANNOT_EMPTY));
		}

		try {
			result = ${lowerEntityName}Mapper.deleteBatchIds(Arrays.asList(${lowerPrimaryKey}s));
		} catch (Exception e){
			throw new DaoException(e, MessageUtils.get(MessageConstant.DELETE_FAILED));
		}

		return result;
	}

	/**
	 * 删除
	 *
	 * @param map
	 * @return 删除条数
	 * @throws DaoException 数据库异常
	 * @throws ValidateException 数据校验异常
	 */
	@Override
	public int deleteByMap(Map<String,Object> map) throws DaoException, ValidateException{
		int result = 0;

		if(map==null || map.size()==0){
			throw new ValidateException("map"+ MessageUtils.get(MessageConstant.PARAMETER_CANNOT_EMPTY));
		}

		try {
			result = ${lowerEntityName}Mapper.deleteByMap(map);
		} catch (Exception e){
			throw new DaoException(e, MessageUtils.get(MessageConstant.DELETE_FAILED));
		}

		return result;
	}

	/**
	 * 查询单个
	 *
	 * @param ${lowerPrimaryKey} ${primaryKeyComment}
	 * @return 查询对象
	 * @throws DaoException 数据库异常
	 * @throws ValidateException 数据库异常
	 */
	@Override
	public ${upperEntityName}Vo selectById(${primaryType} ${lowerPrimaryKey}) throws DaoException,ValidateException {
		${upperEntityName}Vo ${lowerEntityName}Vo = null;

		if(ObjectUtil.isEmpty(${lowerPrimaryKey})){
			throw new ValidateException("${lowerPrimaryKey}"+ MessageUtils.get(MessageConstant.PARAMETER_CANNOT_EMPTY));
		}

		try {
			${lowerEntityName}Vo = ${lowerEntityName}Mapper.selectById(${lowerPrimaryKey});
		} catch (Exception e){
			throw new DaoException(e, MessageUtils.get(MessageConstant.QUERY_FAILED));
		}

		return ${lowerEntityName}Vo;
	}

	/**
	 * 分页查询
	 *
	 * @param ${lowerEntityName}Dto 查询条件
	 * @return 分页对象
	 * @throws DaoException 数据库异常
	 */
	@Override
	public Page<${upperEntityName}Vo> selectPage(${upperEntityName}Dto ${lowerEntityName}Dto) throws DaoException {
		Page<${upperEntityName}Vo> page = new Page<${upperEntityName}Vo>();
		try {
			List<${upperEntityName}Vo> ${lowerEntityName}Vos = ${lowerEntityName}Mapper.selectListByMap(BeanUtil.beanToMap(${lowerEntityName}Dto));
			${lowerEntityName}Dto.clearWrapper();
			long total = ${lowerEntityName}Mapper.selectCountByMap(BeanUtil.beanToMap(${lowerEntityName}Dto));
			page = new Page<${upperEntityName}Vo>(${lowerEntityName}Vos, total);
		} catch (Exception e){
			throw new DaoException(e, MessageUtils.get(MessageConstant.QUERY_FAILED));
		}
		return page;
	}

	/**
	 * 查询集合
	 *
	 * @param ${lowerEntityName}Dto 查询条件对象
	 * @return 查询的集合
	 * @throws DaoException 数据库异常
	 */
	@Override
	public List<${upperEntityName}Vo> selectList(${upperEntityName}Dto ${lowerEntityName}Dto) throws DaoException {
		List<${upperEntityName}Vo> list = null;
		try {
			list = ${lowerEntityName}Mapper.selectListByMap(BeanUtil.beanToMap(${lowerEntityName}Dto));
		} catch (Exception e){
			throw new DaoException(e, MessageUtils.get(MessageConstant.QUERY_FAILED));
		}
		return list;
	}

	/**
	 * 查询总数
	 *
	 * @param ${lowerEntityName}Dto 查询条件对象
	 * @return 查询的总数
	 * @throws DaoException 数据库异常
	 */
	@Override
	public long selectCount(${upperEntityName}Dto ${lowerEntityName}Dto) throws DaoException{
		long total = 0;
		try {
			total = ${lowerEntityName}Mapper.selectCountByMap(BeanUtil.beanToMap(${lowerEntityName}Dto));
		} catch (Exception e){
			throw new DaoException(e, MessageUtils.get(MessageConstant.QUERY_FAILED));
		}
		return total;
	}

}
